package com.liyongjian.xhban.utils;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhougui735  
 * @date 2015年6月29日 下午2:47:25 
 */

public class HttpUtils {
	private static final Logger logger = LoggerFactory
			.getLogger(HttpUtils.class);
	
	private static int  connectTimeout = 10 * 1000; 	//连接host的最大时间
	private static int  connectionRequestTimeout = 30 * 1000;	//从链接池中，去一个连接的最大时间
	private static int  socketTimeout = 180 * 1000;		//一次http请求最大时间
	
	
	public static int getConnectTimeout() {
		return connectTimeout;
	}


	public static void setConnectTimeout(int connectTimeout) {
		HttpUtils.connectTimeout = connectTimeout;
	}


	public static int getConnectionRequestTimeout() {
		return connectionRequestTimeout;
	}


	public static void setConnectionRequestTimeout(int connectionRequestTimeout) {
		HttpUtils.connectionRequestTimeout = connectionRequestTimeout;
	}


	public static int getSocketTimeout() {
		return socketTimeout;
	}


	public static void setSocketTimeout(int socketTimeout) {
		HttpUtils.socketTimeout = socketTimeout;
	}

	public static String post(String url, List<NameValuePair> pairs){
		CloseableHttpClient httpClient = null;
		String result = null;
    	HttpEntity entity = null;
    	try{
	    	RequestConfig rconfig = RequestConfig.custom().setConnectTimeout(connectTimeout) //连接host的最大时间
	                .setConnectionRequestTimeout(connectionRequestTimeout)
	                .setSocketTimeout(socketTimeout)
	                .build();
	        httpClient = HttpClients.custom().setDefaultRequestConfig(rconfig).build();
	        HttpPost post = new HttpPost(url);
	        entity = new UrlEncodedFormEntity(pairs,"utf-8");
	        post.setEntity(entity);
	        HttpResponse response = httpClient.execute(post);
	        entity = response.getEntity();
	        result = EntityUtils.toString(entity);
		}catch(Exception e){
    		logger.error(url + pairs + " post error ",e);
    	}finally{
			if(httpClient != null){
				try {
					httpClient.close();
				} catch (IOException e) {
					logger.error(" post httpClient close error ",e);
				}
			}
		}
    	return result;
	}

	public static String postJson(String url, String pairs){
		CloseableHttpClient httpClient = null;
		String result = null;
    	HttpEntity entity = null;
    	try{
	    	RequestConfig rconfig = RequestConfig.custom().setConnectTimeout(connectTimeout) //连接host的最大时间
	                .setConnectionRequestTimeout(connectionRequestTimeout)
	                .setSocketTimeout(socketTimeout)
	                .build();
	        httpClient = HttpClients.custom().setDefaultRequestConfig(rconfig).build();
	        HttpPost post = new HttpPost(url);
	        entity = new StringEntity(pairs,"utf-8");
	        post.setEntity(entity);
	        HttpResponse response = httpClient.execute(post);
	        entity = response.getEntity();
	        result = EntityUtils.toString(entity);
		}catch(Exception e){
    		logger.error(url + pairs + " postJson error ",e);
    	}finally{
			if(httpClient != null){
				try {
					httpClient.close();
				} catch (IOException e) {
					logger.error(" postJson httpClient close error ",e);
				}
			}
		}
    	return result;
	}
	
	public static String getJson(String url){
		logger.info("get json url:"+url);
		CloseableHttpClient httpClient = null;
		String result = null;
    	HttpEntity entity = null;
    	try{
	    	RequestConfig rconfig = RequestConfig.custom().setConnectTimeout(connectTimeout) //连接host的最大时间
	                .setConnectionRequestTimeout(connectionRequestTimeout)
	                .setSocketTimeout(socketTimeout)
	                .build();
	        httpClient = HttpClients.custom().setDefaultRequestConfig(rconfig).build();
	        HttpGet get = new HttpGet(url);
	        HttpResponse response = httpClient.execute(get);
	        entity = response.getEntity();
	        result = EntityUtils.toString(entity);
		}catch(Exception e){
    		logger.error(url + " getJson error ",e);
    	}finally{
			if(httpClient != null){
				try {
					httpClient.close();
				} catch (IOException e) {
					logger.error(" getJson httpClient close error ",e);
				}
			}
		}
    	return result;
	}
}
